//
//  SuperviseTaskCheckView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/9.
//

import Foundation
import UIKit

class RwjlItemParam {
    var id: Int64? // 任务项ID
    var rwjlid: Int64? // 任务ID
    var rwmbid: Int64?  // 任务模板ID
    var rwzt: String?
    var fjsm: String?
    var fjImgs: [UIImage]?
    
    init(id: Int64? = nil, rwjlid: Int64? = nil, rwmbid: Int64? = nil, rwzt: String? = nil, fjsm: String? = nil, fjImgs: [UIImage]? = nil) {
        self.id = id
        self.rwjlid = rwjlid
        self.rwmbid = rwmbid
        self.rwzt = rwzt
        self.fjsm = fjsm
        self.fjImgs = fjImgs
    }
}

class FjdzParam: Encodable {
    var fjdz: String
    init(fjdz: String) {
        self.fjdz = fjdz
    }
}

class RwjlMbParam: Encodable {
    var id: Int64? // 任务项ID
    var rwjlid: Int64? // 任务ID
    var rwmbid: Int64?  // 任务模板ID
    var rwzt: String?
    var fjsm: String?
    var rwjlMbFjs: [FjdzParam]?
    
    init(item: RwjlItemParam, images: [FjdzParam]? = nil) {
        self.id = item.id
        self.rwjlid = item.rwjlid
        self.rwmbid = item.rwmbid
        self.rwzt = item.rwzt
        self.fjsm = item.fjsm
        self.rwjlMbFjs = images
    }
}

class RwKsjctjParam: Encodable {
    var id: Int64
    var xjrqms: [FjdzParam]?
    var bjrqz: String?
    var lng: String?
    var lat: String?
    var location: String?
    var rwjlMbs: [RwjlMbParam]?
    
    init(id: Int64, xjrqms: [FjdzParam]? = nil, bjrqz: String? = nil, lng: String? = nil, lat: String? = nil, location: String? = nil, rwjlMbs: [RwjlMbParam]? = nil) {
        self.id = id
        self.xjrqms = xjrqms
        self.bjrqz = bjrqz
        self.lng = lng
        self.lat = lat
        self.location = location
        self.rwjlMbs = rwjlMbs
    }
}

class SuperviseTaskCheckView: UIView {
    let submitBtn = UIButton.createPrimaryLarge("评估结果并提交")

    let contentV = UIView()

    let sign1TF = SignEffect()
    let sign2TF = SignEffect()
    let sign3TF = SignEffect()
    
    var rwjlMbs: [RwjlItemParam] = []

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal modal: SpRwbgModal?) {
        contentV.removeAllSubViews()
        
        let datas = modal?.rwjlMbs ?? []
        
        var lastI: SuperviseCheckEffect?
        for (index, mb) in datas.enumerated() {
            let itemV = SuperviseCheckEffect()
            itemV.reload(withModal: mb)
            itemV.didSelectItemWith = { (rwzt, msg, imgs) -> () in
                var index = -1
                for (i, zg) in self.rwjlMbs.enumerated() {
                    if zg.id == mb.id {
                        index = i
                    }
                }
                if index > -1 {
                    self.rwjlMbs.remove(at: index)
                }
                self.rwjlMbs.append(RwjlItemParam(id: mb.id, rwjlid: modal?.id, rwmbid: mb.rwmbid, rwzt: rwzt, fjsm: msg, fjImgs: imgs))
            }
            contentV.addSubview(itemV)
            itemV.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                make.top.equalTo(lastI == nil ? contentV.snp.top : lastI!.snp.bottom).offset(10)
                if index == datas.count - 1 {
                    make.bottom.equalToSuperview()
                }
            }
            lastI = itemV
        }
    }
    
    func setupUI() {
        addSubview(submitBtn)
        submitBtn.snp.makeConstraints { (make) -> Void in
            make.bottom.equalToSuperview().offset(-14)
            make.height.equalTo(50)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        let scrollV = UIScrollView()
        scrollV.keyboardDismissMode = .interactive
        scrollV.showsVerticalScrollIndicator = false
        addSubview(scrollV)
        scrollV.snp.makeConstraints { (make) -> Void in
            make.top.equalToSuperview()
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth)
            make.bottom.equalTo(submitBtn.snp.top).offset(-26)
        }
    
        scrollV.addSubview(contentV)
        contentV.snp.makeConstraints { (make) -> Void in
            make.top.left.equalToSuperview()
            make.width.equalTo(ScreenWidth)
        }
        
        sign1TF.placeholder = "检查人签字1"
        scrollV.addSubview(sign1TF)
        sign1TF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(contentV.snp.bottom).offset(10)
            make.centerX.equalTo(self.snp.centerX)
            make.height.equalTo(50)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        sign2TF.placeholder = "检查人签字2"
        scrollV.addSubview(sign2TF)
        sign2TF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(sign1TF.snp.bottom).offset(10)
            make.centerX.equalTo(self.snp.centerX)
            make.height.equalTo(50)
            make.width.equalTo(ScreenWidth - 20)
        }
        
        sign3TF.placeholder = "被检查人签字"
        scrollV.addSubview(sign3TF)
        sign3TF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(sign2TF.snp.bottom).offset(10)
            make.centerX.equalTo(self.snp.centerX)
            make.height.equalTo(50)
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalToSuperview()
        }
    }
}
